package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Permission;
import com.example.entity.Role;
import com.example.mapper.PermissionMapper;
import com.example.service.PermissionService;
import com.example.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RoleService roleService;
    //根据role的id获得权限列表
    public List<Permission> getByRoleList(List<Long> roleIdList) {
        Long flag;

        if (roleIdList.contains(1)){
            flag = 1L;
        } else if(roleIdList.contains(3)){
            flag = 3L;
        } else {
            flag = 2L;
        }
        //根据角色id查询到该角色拥有的权限
        LambdaQueryWrapper<Role> select = new QueryWrapper<Role>().lambda().eq(Role::getId,flag);
        Role one = roleService.getOne(select);

        //通过查询角色找到相关权限
        List<Long> permission = one.getPermission();
        if (permission.size()!=0){
            LambdaQueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>().lambda().in(Permission::getId,permission );
            return list(queryWrapper);
        }

        return new ArrayList<>();

    }
    //根据role的列表获得权限列表
    public List<Permission> getByRoles(List<Role> roles) {
        Long flag =0L;
        for (Role role : roles) {
            if (role.getId().equals(1L)){
                flag = 1L;
                break;
            } else if(role.getId().equals(3L)){
                flag = 3L;
                break;
            } else {
                flag = 2L;
                break;
            }
        }
        //根据角色id查询到该角色拥有的权限
        LambdaQueryWrapper<Role> select = new QueryWrapper<Role>().lambda().eq(Role::getId,flag);
        Role one = roleService.getOne(select);

        //通过查询角色找到相关权限
        LambdaQueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>().lambda().in(Permission::getId,one.getPermission() );

        return list(queryWrapper);
    }

    @Transactional
    public void delete(Long id) {
        removeById(id);
        // 删除角色分配的菜单
        List<Role> list = roleService.list();
        for (Role role : list) {
            // 重新分配权限
            List<Long> newP = new ArrayList<>();
            for (Object p : role.getPermission()) {
                Long pl = Long.valueOf(p + "");
                if (!id.equals(pl)) {
                    newP.add(Long.valueOf(p + ""));
                }
            }
            role.setPermission(newP);
            roleService.updateById(role);
        }
    }

}
